package com.myblog.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myblog.Utils.BeanCopyUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.Vo.PageVo;
import com.myblog.domain.Vo.RoleVo;
import com.myblog.domain.contants.GlobalConstants;
import com.myblog.domain.dto.AddRoleDto;
import com.myblog.domain.dto.RoleDto;
import com.myblog.domain.dto.UpdateRoleDto;
import com.myblog.domain.entity.Role;
import com.myblog.domain.entity.RoleMenu;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.mapper.RoleMapper;
import com.myblog.services.RoleMenuService;
import com.myblog.services.RoleService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMenuService roleMenuService;
    @Override
    public List<String> getRoleList(Long id) {
        if(id==1L){
            List<String> list=new ArrayList<>();
            list.add("admin");
            return list;
        }
        return getBaseMapper().selectRoleById(id);
    }

    @Override
    public GlobalResponse saveRole(AddRoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        save(role);
        List<String> menuIdList=roleDto.getMenuIds();
        List<RoleMenu> roleMenuList = menuIdList.stream()
                .map( menuId -> new RoleMenu(role.getId(), Long.parseLong(menuId)))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);

        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse getList(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.hasText(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(Strings.hasText(status),Role::getStatus,status);
        Page page=new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<RoleVo> list = BeanCopyUtils.copyBeanList(page.getRecords(), RoleVo.class);
        PageVo pageVo=new PageVo(list,page.getTotal());
        return GlobalResponse.ok(pageVo);
    }

    @Override
    public GlobalResponse changeStatus(RoleDto roleDto) {
        Long id=roleDto.getRoleId();
        String status=roleDto.getStatus();
        if(status.equals(GlobalConstants.STATUS_NORMAL)||status.equals(GlobalConstants.STATUS_NOT_NORMAL)){
            updateById(new Role(id,status));
            return GlobalResponse.ok();
        }
        return GlobalResponse.error(GlobalResponseEnum.ERROR.code,"状态只能为0或1");
    }

    @Override
    public GlobalResponse updateRole(UpdateRoleDto updateRoleDto) {
        Role role = BeanCopyUtils.copyBean(updateRoleDto, Role.class);
        updateById(role);

        List<String> menuIds = updateRoleDto.getMenuIds();

        List<RoleMenu> roleMenuList = menuIds.stream()
                .map(s -> new RoleMenu(role.getId(), Long.parseLong(s)))
                .collect(Collectors.toList());


        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.remove(roleMenuLambdaQueryWrapper);
        roleMenuService.saveBatch(roleMenuList);


        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse deleteRole(Long id) {
        boolean b = removeById(id);
        if(!b){
            return GlobalResponse.error(GlobalResponseEnum.ID_NO_EXIST);
        }
        return GlobalResponse.ok();
    }

    @Override
    public GlobalResponse getAllRole() {
        List<Role> list = list();
        return GlobalResponse.ok(list);
    }
}
